<?php  if (!defined('PATH_HD')) exit('No direct script access allowed');
	//图片管理
	class ImageControl extends Control{
		//图片管理界面
		function index(){
			$db = K("image");
			$result = $db->all();
			$this->assign("data",$result);
			$this->display();
		}

		//添加图片
		function addShow(){
			//如果没有选择栏目则分配所有栏目到视图
			if(!isset($_GET['cid'])){
				$this->assignCategorydata();	
			}
			$this->display();
		}

		//处理添加的图片到数据库
		function add(){
			$data = $_POST;
			$data['thumbimg'] = isset($data['thumbimg'][1]) ? $data['thumbimg'][1]['path'] : '';//缩略图
			$db = M("image");
			if($db->add($data)){
				$this->success("添加图片成功！","index");
			}else{
				$this->error("添加图片失败 ，请重试！","add");
			}
		}

		//图片编辑视图
		function editShow(){
			if($iid = $this->_get('iid')){
				$db = M("image");
				$image = $db->find($iid);
				$thumbimg = array(
	                array(
	                    "alt" => $image['keyword'],
	                    "path" => $image['thumbimg'],
	                    "thumb" => array($image['thumbimg'])
	                )
            	);
           		$this->assign("thumbimg", $thumbimg);
            	$this->assignCategorydata();
            	$this->assign("data", $image);
			}
			$this->display();
		}
		//修改图片数据
		function save(){
			C("debug",1);
			if($this->_post('title')){
				$db = M("image");
				$data = array();
				$data = $_POST;
				$data['thumbimg'] = isset($_POST['thumbimg'][1]['path']) ? $_POST['thumbimg'][1]['path'] : '';
				if($db->save($data)){
					$this->success("编辑图片成功！","index");
				}else{
					$this->error("编辑图片出错，请重试！");
				}
			}
		}
		//异步删除图片文件
		function delImg(){
			if(isset($_POST['iid'])){
				$db = M("image");
				$iid = $_POST['iid'];
				$result = $db->del("iid='".$iid."'");
				$data = array();
				if($result){
					$data['state'] = "";
					$data['msg'] = "删除图片成功！";
				}else{
					$data['state'] = "er";
					$data['msg'] = "删除图片失败，请重试！";
				}
				echo json_encode($data);

			}
			

		}
		//获取栏目数据并分配到视图
		function assignCategorydata(){
			$db = M("category");
			$category = $db->field("cid,catname")->all();
			$this->assign("category",$category);
		}
	}
?>